package com.example.csclient.ui

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.csclient.ui.screens.CscUiState
import com.example.csclient.ui.screens.CscViewModel
import com.example.csclient.ui.screens.LoginScreen
import com.example.csclient.ui.screens.MainScreen

/**
 * @brief 智能充电桩 APP 主界面
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CscApp() {
    val cscViewModel: CscViewModel = viewModel(factory = CscViewModel.Factory)
    val uiState by cscViewModel.cscUiState.collectAsState()
    val appData by cscViewModel.appData.collectAsState()

    Scaffold(
        topBar = {
            CscTopAppBar(
                cscUiState = uiState,
                onRechargeClicked = {
                    cscViewModel.handleRechargeButtonPush()
                },
                onStopClicked = {
                    if (uiState.selectedOption != "停止充电") {
                        cscViewModel.handleSelectButtonClick("停止充电")
                    } else {
                        cscViewModel.handleSelectButtonClick("退出登陆")
                    }
                }
            )
        },
        modifier = Modifier.fillMaxSize()
    ) { innerPadding ->
        if (uiState.loginState) {
            MainScreen(
                modifier = Modifier.padding(innerPadding),
                cscUiState = uiState,
                appData = appData,
                onConfirmReservation = { timePickerState, willChargeAfterScheduleAttach ->
                    cscViewModel.handleReservation(
                        timePickerState, willChargeAfterScheduleAttach
                    )
                },
                onDismissReservation = { cscViewModel.willShowReservation(false) },
                onConfirmHandModelAdjust = { handModelPercent, powerAdjustPercent ->
                    cscViewModel.handleHandModelAdjust(
                        handModelPercent = handModelPercent,
                        powerAdjustPercent = powerAdjustPercent
                    )
                },
                onCancelReservation = { cscViewModel.handleCancelReservation() },
                onShowReservation = { cscViewModel.willShowReservation(true) },
                onOptionSelected = { option ->
                    cscViewModel.handleSelectButtonClick(option)
                },
                onConfirmRechargeMoney = { rechargeMoney ->
                    cscViewModel.handleRechargeRequest(rechargeMoney)
                }
            )
        } else {
            LoginScreen(
                modifier = Modifier.padding(innerPadding),
                onConnectToEmbeddedDevice = { userName, password, embeddedDeviceID ->
                    cscViewModel.handleConnectEmbeddedDevice(userName, password, embeddedDeviceID)
                },
                onRegisterUser = { userName, password ->
                    cscViewModel.handleRegisterUser(userName, password)
                },
                serverResponse = uiState.loginPageResponse
            )
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CscTopAppBar(
    cscUiState: CscUiState,
    onRechargeClicked: () -> Unit,
    onStopClicked: () -> Unit
) {
    CenterAlignedTopAppBar(
        colors = TopAppBarDefaults.topAppBarColors(
            containerColor = MaterialTheme.colorScheme.primaryContainer,
            titleContentColor = MaterialTheme.colorScheme.primary
        ),
        title = {
            Text(
                "智能充电桩",
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                fontWeight = FontWeight.Bold
            )
        },
        navigationIcon = {
            TextButton(
                onClick = { onRechargeClicked() },
                enabled = cscUiState.loginState
            ) {
                Text("充值")
            }
        },
        actions = {
            TextButton(
                onClick = { onStopClicked() },
                enabled = cscUiState.loginState
            ) {
                Text("停止")
            }
        }
    )
}
